package api_servers
{
	import flash.display.DisplayObjectContainer;
	import flash.display.LoaderInfo;
	import flash.net.URLVariables;
	

	public class api_mailserver extends api_externalserver {
	    public static var wrapper: Object = null;
		public static var STAGE:* = null;

	    private static var MY_APP_ID : String = '441805'; // Рамки
//	    public static const MY_APP_ID : String = '456998'; // Рамки test
//	    private static const MY_SECRET : String = "2567b2f9c525b55c8fd0b226df3d06b7";

	     
/*	    private static const MY_APP_ID : String = '430262'; // Грузчик 2
	    private static const MY_SECRET : String = "dd0b7ea38f1d737bd5954519db8ce756";*/

	    
	    private static const VER:String 	= "0.1";
//	    public static  const MY_ID: String 	= '13516051446856029964'; //14475128; // 14473216
//	    public static  const MY_ID: String 	= '8062938299454250872'; //14475128; // 14473216
//	    public static  const MY_ID: String 	= '1731353195984349210'; //14475128; // 14473216
	    private static const SERVER : String = 'http://www.appsmail.ru/platform/api';
		protected var _session_key	: String;
		protected var _adminList	: Array; 
		
		public static const MAIL_ERROR: String = 'MAIL_ERROR';
		
		public function api_mailserver(root: DisplayObjectContainer) {
			var a_vars: Object = LoaderInfo(root.stage.loaderInfo).parameters;
			
			var app_id:String = a_vars["app_id"];
			if (!app_id) app_id = MY_APP_ID;
			
			super(root, SERVER, '', app_id, secrets[app_id]);
			vars = a_vars;
			
			viewer_id = vars["vid"];
			user_id	= viewer_id?viewer_id:vars["oid"];
			
			_session_key = vars["session_key"];

/*			if (!_session_key) _session_key = 'fb69b11a963dcf6fd7acdc05f2a5c31e';
			if (!vars["window_id"]) vars["window_id"] = 'CometName_e658ce36675b262e898dc24d38dc3a16';

			if ( !parseInt(viewer_id)) {// Local testing 
				viewer_id 	= MY_ID;
			    user_id 	= MY_ID;
			}*/
			  
			if ( !parseInt(user_id)) user_id = viewer_id;
			_adminList = getAdminList();
			
		}
		
		public function get secrets(): Object {
			return {429390:'2567b2f9c525b55c8fd0b226df3d06b7',
					430262:'dd0b7ea38f1d737bd5954519db8ce756',
					441805:'3b0c9d411eb5c5dc252c9fe26d4da53a',
					456998:'0653cbe9091b1c69647af268657e703b',
					464341:'0ed3abf21357a5330738895e2412a984',
					490280:'28a7ac8f177d64a88fe45453aca8f79e'}
		}
		
		protected function getAdminList(): Array {
			return ['8062938299454250872', 
					'17202575027144026534', 
					'14095186048222604222']
		}
		
	    override protected function prepareData(a_queryID:String, data:*):* {
	    	if (data.error) {
	    		data.error.queryID = a_queryID;
	    		sroot.dispatchEvent(new GDataEvent(MAIL_ERROR, data.error));
	    		return null;
	    	} else return super.prepareData(a_queryID, data);
	    }
	    
		override public function get flashVars(): Object {
			return vars;
		}
		
		override public function isAdmin(): Boolean {
			return getIsAdmin();
		}
		
		protected function getIsAdmin(): Boolean {
			return _adminList.indexOf(viewer_id) > -1;
		}
		
	    private function getFlashVarInt( s:String ):int {
	      return (vars[s] != null)
	        ?	parseInt( vars[s] ) 
	        :	0;
	    }
	    
		override protected function getSig( arr: Array): String {
			var sig1: String = viewer_id;
			var sig2: String = '';
			for( var i:uint = 0; i < arr.length; i++ ) {
				sig2 += arr[i].pn + '=' + arr[i].pv;
				if (arr[i].pn == 'uid') sig1 = arr[i].pv;
			}
			return sig1 + sig2 + secret;
		}
		
		override protected function srHelper( arr:Array): URLVariables {// sendRequestHelper
			arr.push( {pn: "app_id", pv: app_id } );
			var i:int;
			if (_session_key) {
				var index: int = -1;
				for (i = 0; i<arr.length; i++) {
					if (arr[i].pn == 'session_key') index = i;
				};
				if (index == -1) arr.push( {pn: "session_key", pv: _session_key } );
				else if (arr[index].pv == 'none') 
					arr.splice(index, 1);
			}
					  
			arr.sortOn('pn');
					  
			var url_vars:URLVariables = new URLVariables();
			for( i = 0; i < arr.length; i++ ) {
				url_vars[ (arr[i].pn) ] = arr[ i ].pv;
			}
					  
			url_vars["sig"] = makeSig( arr );
			return url_vars;
		}
	    
	    override public function getProfiles(uids: String, handler_:*, fields: String='', queryID:String=''): void {
	    	function myReceive(data: *): void {
	    		data = data[0];
	    		if (data && data.nick) {
	    			data.user_id = data.uid;
		    		data.sex = 2 - data.sex;
		    		data.photo = data.pic;
		    		data.city = data.city?data.city:0;
		    		data.nickname = data.nick;
		    		handler_(data);
		    	} else handler_(undefined);
	    	}
	    	jsrHelper([
						{pn: "method", pv: "users.getInfo"},
						{pn: 'uids', pv: uids}
	    			], myReceive, queryID);
	    }

	    override public function getInstall(uuids: String, handler_:*, queryID:String='', a_app_id: String=''): void {
	    	jsrHelper([
	    				{pn: "uid", pv: uuids},
	    				{pn: "session_key", pv: 'none'},
						{pn: "method", pv: "users.isAppUser"}
	    			], handler_, queryID);	    	
	    }
	    
	    override public function getFriends(uid: String, handler_:*, queryID:String=''): void {
	    	jsrHelper([
						{pn: "method", pv: "friends.getAppUsers"}
	    			], handler_, queryID);	    	
	    }
	    
	    override public function streamPublish(text: String, user_text: String, handler_:*=null, queryID:String='', img: int=66): void {
	    	jsrHelper([
						{pn: "method", pv: "stream.publish"},
						{pn: "text", pv: text},
						{pn: "post", pv: user_text},
						{pn: "img", pv: img}
	    			], handler_, queryID);	    	
	    }
	}
}